//
//  NSObject+ext.swift
//  GRVoice
//
//  Created by  on 2021/10/12.
//  Copyright © 2021 国瑞语音. All rights reserved.
//

import UIKit
//MARK: - 获取对象的类名
extension NSObject
{
    // 对象的类名
    var className : String
    {
        get
        {
            let name = type(of: self).description()
            if name.contains(".")
            {
                return name.components(separatedBy: ".").last!
            }
            else
            {
                return name
            }
        }
    }
    
    static var className : String
    {
        get
        {
            let name = self.description()
            if name.contains(".")
            {
                return name.components(separatedBy: ".").last!
            }
            else
            {
                return name
            }
        }
    }
    
    @objc func getKeyWindow()->UIWindow
    {
        let window = (UIApplication.shared.delegate?.window ?? UIApplication.shared.keyWindow)
        //        assert(window == nil, "\(self.className)没有找到window")
        return window!
    }
    
     func currentVC()->UIViewController
    {
        var result : UIViewController?=nil
        var window = self.getKeyWindow()
        if window.windowLevel != .normal{
            let windows = UIApplication.shared.windows
            for tmpwindow in windows{
                if tmpwindow.windowLevel == .normal{
                    window = tmpwindow
                    break
                }
            }
        }
        
        result = window.rootViewController
        while ((result!.presentedViewController) != nil)
        {
            result = result!.presentedViewController
        }
        if result!.isKind(of: UITableViewController.self){
            result = (result as! UITableViewController).tabBarController?.selectedViewController
        }
        if result!.isKind(of: UINavigationController.self){
            result = (result as! UINavigationController).visibleViewController
        }
        return result!
    }
}


//MARK:- 一些判断
extension NSObject
{
    func isNull() -> Bool
    {
        if self == NSNull() {
            return true
        }
        else if self == nil{
            return true
        }
        else if self.isKind(of:NSArray.classForCoder()){
            return (self as! NSArray).count == 0
        }
        else if self.isKind(of:NSMutableArray.classForCoder()){
            return (self as! NSMutableArray).count == 0
        }
        else if self.isKind(of:NSDictionary.classForCoder()){
            return (self as! NSDictionary).count == 0
        }
        else if self.isKind(of:NSMutableDictionary.classForCoder()){
            return (self as! NSMutableDictionary).count == 0
        }
        else if self.isKind(of:NSData.classForCoder()){
            return (self as! NSData).length == 0
        }
        else if self.isKind(of:NSString.classForCoder()){
            return (self as! NSString).length == 0
        }
        else if self.isKind(of:NSMutableString.classForCoder()){
            return (self as! NSMutableString).length == 0
        }
        else if self.isKind(of:NSSet.classForCoder()){
            return (self as! NSSet).count == 0
        }
        else if self.isKind(of:NSNumber.classForCoder()){
            return "\(self)".count == 0
        }
        return false
    }
}

//MARK: - 生成二维码
extension NSObject{
   @objc func qr(_ content:String,_ qrColor:UIColor, _ bgColor:UIColor)->UIImage{
        
        let data = content.data(using:String.Encoding.utf8)
        
        let qr = CIFilter(name:"CIQRCodeGenerator")
        qr?.setValue(data, forKey: "inputMessage")
        qr?.setValue("H", forKey: "inputCorrectionLevel")
        
        var ciimg = qr?.outputImage
        ciimg = ciimg?.transformed(by: CGAffineTransform(scaleX:10, y: 10))
        
        ///设置颜色
        let colorfilter = CIFilter(name: "CIFalseColor")
        colorfilter?.setValue(ciimg, forKey:"inputImage")
        ///二维码颜色
        colorfilter?.setValue(CIColor(cgColor:qrColor.cgColor), forKey: "inputColor0")
        ///背景颜色
        colorfilter?.setValue(CIColor(cgColor:bgColor.cgColor), forKey: "inputColor1")
        
        ciimg = colorfilter?.outputImage
        return UIImage(ciImage: ciimg!)
    }
}
